package game.platformer;

import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import game.platformer.interfaces.screenManager;
import game.platformer.levels.levelExit;
import game.platformer.levels.levelHazards;
import game.platformer.levels.levelManager;
import java.util.LinkedList;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

public class platformer extends BaseGameActivity
{
	//constants----------------------------------
	public static final int cameraWidth = 720;
	public static final int cameraHeight = 480;
	//-------------------------------------------
	//varblock-----------------------------------
	static screenManager scnMan;
	static levelManager lman;
	static PhysicsWorld pworld;
	int camWidth = 720;
	int camHeight = 480;
	Camera viewport;
	//-------------------------------------------

	public static screenManager getScreenManager(){
		assert (scnMan != null);
		return scnMan;
	}
	public static PhysicsWorld getPhysicsWorld(){
		assert (pworld != null);
		return pworld;
	}
	public static levelManager getLevelManager(){
		assert (lman != null);
		return lman;
	}

	public Engine onLoadEngine(){
		camWidth = 720;
		camHeight = 480;
		viewport = new Camera(0, 0, 720, 480);
		//load up the engine
		EngineOptions eopt = new EngineOptions(true,
			EngineOptions.ScreenOrientation.LANDSCAPE,
			new RatioResolutionPolicy(camWidth, camHeight), viewport);
		eopt.getTouchOptions().setRunOnUpdateThread(true);
		pworld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), true);
		return new Engine(eopt);
	}
	
	public void onLoadResources(){
		//set up a player-------------------
		//load tex's
		Texture playerTex = new Texture(32, 32);
                Texture forwardExitTex = new Texture(32,32);
                Texture fireHazardTex = new Texture (32,32);
		TextureRegion playerTR = TextureRegionFactory.createFromAsset(playerTex, this, "gfx/face_box.png", 0, 0);
                TextureRegion fireHazardTR = TextureRegionFactory.createFromAsset(fireHazardTex, this, "fire/fireHazardFire.png", 0, 0);
                TextureRegion forwardExitTR = TextureRegionFactory.createFromAsset(forwardExitTex, this, "gfx/forwardExit.png",0,0);
                
                
		//load tex's to engine, bodys to pw
		getEngine().getTextureManager().loadTexture(playerTex);
                getEngine().getTextureManager().loadTexture(forwardExitTex);
                getEngine().getTextureManager().loadTexture(fireHazardTex);
                //sprites
		Sprite ps = new Sprite(0, 0, playerTR);
                Sprite exitSprite = new Sprite (0,0,forwardExitTR);
                Sprite fireHazardSprite = new Sprite (0,0,fireHazardTR);
                
		//physics body
		FixtureDef f = PhysicsFactory.createFixtureDef(1f, 0f, .5f);
		Body b = PhysicsFactory.createBoxBody(pworld, ps, BodyType.DynamicBody, f);
                
		//init player
		Player.initialize(ps, b);
                
		//init exit sprite------------------
		levelExit.initializeExitSprite(forwardExitTR);
                
                //init hazard sprites
                levelHazards.initializeFireHazardSprite(fireHazardTR);
		//----------------------------------
		//set up managers-------------------
		scnMan = new screenManager(this, viewport);
		lman = new levelManager(this);
		//----------------------------------
	}

	public Scene onLoadScene(){
		return getScreenManager().getMainMenuScreen();
	}

	public void onLoadComplete(){
		//do nothing
	}	
}
